package com.nhncorp.pleiades.util;

import java.net.InetSocketAddress;

public class NetworkUtil {
  public static InetSocketAddress getAddress(String hostStr) {
    String target = hostStr;
    int colonIndex = target.indexOf(':');
    if (colonIndex < 0) {
      throw new RuntimeException("Not a host:port pair: " + hostStr);
    }
    String host = target.substring(0, colonIndex);
    int port = Integer.parseInt(target.substring(colonIndex + 1));

    return new InetSocketAddress(host, port);
  }

  public static String getHostNameToDir(String hostName) {
    int index = hostName.indexOf(":");
    if (index < 0)
      return hostName;

    return hostName.substring(0, index) + "_" + hostName.substring(index + 1);
  }

  public static String getHostNameToDirWithLastIndexOf(String hostName) {
    int index = hostName.lastIndexOf(":");
    if (index < 0)
      return hostName;

    return hostName.substring(0, index) + "_" + hostName.substring(index + 1);
  }

  public static String getHostNameFromDir(String hostName) {
    int index = hostName.indexOf("_");
    if (index < 0)
      return hostName;

    return hostName.substring(0, index) + ":" + hostName.substring(index + 1);
  }
}
